package com.springboot.service;

import com.springboot.dao.*;
import com.springboot.dao.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018\9\8 0008.
 */
@Service
public class UserService {

    @Autowired
    private UserEntityMapper userEntityMapper;
    @Autowired
    private User_moduleMapper user_moduleMapper;
    @Autowired
    private User_branchMapper user_branchMapper;
    @Autowired
    private Modeule_nameMapper modeule_nameMapper;
    @Autowired
    private Branch_nameMapper branch_nameMapper;

    /**
     * 添加用户信息
     * @param userEntity
     * @return
     */
    public int addUserInfo(UserEntity userEntity) {
       int result =  userEntityMapper.insert(userEntity);
       return result;
    }

    /**
     * 根据用户id查询用户信息
     * @param map
     * @return
     */
    public UserEntity queryUserInfo(Map<String, Object> map) {
        UserEntity userEntity = null;
        if(map.get("user") != null) {
            userEntity = userEntityMapper.selectByPrimaryKey(map.get("user").toString());

        }

        return userEntity;
    }

    /**
     * 后台管理
     * @param map
     * @return
     */
    public Object back_management(Map<String, Object> map) {
        Object object=null;
        UserEntity userEntity = queryUserInfo(map);
//用户信息
        Map<String, String> testMap = new HashMap<String, String>();
        testMap.put("userid",(userEntity.getUserid().toString()));
        testMap.put("user",(userEntity.getUser().toString()));
        testMap.put("password",(userEntity.getPassword().toString()));
//用户显示模块
        User_module user_module = back_stage_management(testMap);
        ArrayList list_branch= new ArrayList();
//显示模块的数组的字符串
        String user_module_str_all= user_module.getModuleState().toString();
//每个模块对应的子页和对应的字段
        for (int i= 0;i<user_module_str_all.split(",").length;i++) {
                ArrayList list_branch_subpage= new ArrayList();
                Map<String, Object> outermost = new HashMap<String, Object>();
                Map<String, Object> testMap_name = new HashMap<String, Object>();
//    子页对应的字段
           for (int j=0;j<back_stage_management_branch(user_module_str_all.split(",")[i]).getBranch().split(",").length;j++){
                String string_branch=back_stage_management_branch(user_module_str_all.split(",")[i]).getBranch();
                list_branch_subpage.add( back_stage_management_branch_name(string_branch.toString().split(",")[j]));
            }
//            整合模块与子页
            testMap_name.put("module",back_stage_management_module_name(user_module_str_all.split(",")[i]).getModeuleName());
            testMap_name.put("List",list_branch_subpage);
            outermost.put("Map",testMap_name);
            list_branch.add(outermost);
        }
//     把模块与子页放入对象中
        object=list_branch;
        return object;
    }

    public User_module back_stage_management(Map<String, String> map) {

        User_module user_module=null;
        if(map.get("userid") != null) {
            user_module = user_moduleMapper.selectByPrimaryKey(map.get("userid"));

        }

        return user_module;
    }
    public User_branch back_stage_management_branch(String string) {

        User_branch user_branch=null;
        if(string != null) {
            user_branch = user_branchMapper.selectByPrimaryKey(string);

        }

        return user_branch;
    }
    public Branch_name back_stage_management_branch_name(String string) {


        Branch_name branch_name=null;
        if(string != null) {
            branch_name = branch_nameMapper.selectByPrimaryKey(string);

        }

        return branch_name;
    }
    public Modeule_name back_stage_management_module_name(String string) {


        Modeule_name modeule_name=null;
        if(string != null) {
            modeule_name = modeule_nameMapper.selectByPrimaryKey(string);

        }

        return modeule_name;
    }

    /**
     * 全部信息
     * @param
     * @return
     */
    public List<Modeule_name> set_up_all() {


        List<Modeule_name> list= modeule_nameMapper.set_up_all();

        return list;
    }

    /**
     * 根据用户模块个数显示
     * @param map
     * @return
     */
    public int Update_module_data( Map<String,Object> map) {
        UserEntity userEntity = null;
        if(map.get("user") != null) {
            userEntity = userEntityMapper.selectByPrimaryKey(map.get("user").toString());
        }
        User_module user_module=null;
        user_module = user_moduleMapper.selectByPrimaryKey(userEntity.getUserid().toString());

        user_module.setModuleState(map.get("moduleState").toString());
        int result = user_moduleMapper.updateByPrimaryKey(user_module);
        return result;
    }

    /**
     * 根据用户模块个数显示状态
     * @param map
     * @return
     */
    public List module_state_data(Map<String,Object> map) {
        UserEntity userEntity = null;
        if(map.get("user") != null) {
            userEntity = userEntityMapper.selectByPrimaryKey(map.get("user").toString());
        }
        User_module user_module=null;
        user_module = user_moduleMapper.selectByPrimaryKey(userEntity.getUserid().toString());
        ArrayList list_ModuleState= new ArrayList();

        for (int i=0;i<user_module.getModuleState().toString().split(",").length;i++){

            Map<String, String> ModuleState_map = new HashMap<String, String>();
            ModuleState_map.put("ModuleState",user_module.getModuleState().toString().split(",")[i]);
            list_ModuleState.add(ModuleState_map);

        }

        return list_ModuleState;
    }




    /**
     * 根据用户id修改用户信息
     * @param userEntity
     * @return
     */
    public int modifyUserInfo(UserEntity userEntity) {
        int result = userEntityMapper.updateByPrimaryKey(userEntity);
        return result;
    }

    /**
     * 根据用户id删除用户信息
     * @param map
     * @return
     */
    public int deleteUserInfo(Map<String, Object> map) {
        int result = 0;
        if(map.get("id") != null) {
            result = userEntityMapper.deleteByPrimaryKey(map.get("id").toString());
        }
        return result;
    }
}
